Console = {
   preferencesService: Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("xmlapp.helper."),
   _initialized: false,
   _debug: false,
   service: Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService),
   log: function(message) {
      this.service.logStringMessage(message);
   },
   debug: function(message) {
      if (!this._initialized) {
         if (Console.preferencesService) {
            try {
               Console._debug = Console.preferencesService.getBoolPref("debug");
            } catch (ex) {
               // no preference
            }
         }
         this._initialized = true;
      }
      if (this._debug) {
         this.service.logStringMessage(message);
      }
   }
}

function ContentSniffer() {
   
}

ContentSniffer.prototype.getMIMETypeFromContent = function(request,data,length) {
   if (request) {
      try {
         var service = Components.classes["@milowski.com/xmlapp;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
         var channel = request.QueryInterface(Components.interfaces.nsIChannel);
         //Console.log("Content Type: "+channel.contentType);
         var typeInfo = service.types[channel.contentType];
         if (typeInfo && typeInfo.inspect) {
            //Console.log("Mapped to: "+typeInfo.mappedType);
            return typeInfo.mappedType;
         }
      } catch (ex) {
         Console.log(ex);
      }
   }
   return null;
}

ContentSniffer.prototype.QueryInterface = function(iid) {
   if (iid.equals(Components.interfaces.nsISupports) ||
       iid.equals(Components.interfaces.nsIContentSniffer)) {
      return this;
   }
   throw Components.results.NS_ERROR_NO_INTERFACE;
};

const CLASSID = Components.ID("{462f09c2-3573-48a1-a08f-ed2998cff7fb}");
const CLASSNAME = "Atom Content Sniffer to Right the Wrongs";
const CONTRACTID = "@milowski.com/atom-content-sniffer;1";

function GenericComponentFactory(ctor, params) {
  this._ctor = ctor;
  this._params = params;
}
GenericComponentFactory.prototype = {
  _ctor: null,
  _params: null,

  createInstance: function GCF_createInstance(outer, iid) {
    if (outer != null)
      throw Cr.NS_ERROR_NO_AGGREGATION;
    return (new this._ctor(this._params)).QueryInterface(iid);
  },

  QueryInterface: function GCF_QueryInterface(iid) {
    if (iid.equals(Components.interfaces.nsIFactory) ||
        iid.equals(Components.interfaces.nsISupports))
      return this;
    throw Cr.NS_ERROR_NO_INTERFACE;
  }
};


var Module = {
  QueryInterface: function M_QueryInterface(iid) {
    if (iid.equals(Components.interfaces.nsIModule) ||
        iid.equals(Components.interfaces.nsISupports))
      return this;
    throw Components.results.NS_ERROR_NO_INTERFACE;
  },

  getClassObject: function M_getClassObject(cm, cid, iid) {
    if (!iid.equals(Components.interfaces.nsIFactory))
      throw Components.results.NS_ERROR_NOT_IMPLEMENTED;

    return new GenericComponentFactory(ContentSniffer);
  },

  registerSelf: function M_registerSelf(cm, file, location, type) {
    var cr = cm.QueryInterface(Components.interfaces.nsIComponentRegistrar);

    cr.registerFactoryLocation(CLASSID, CLASSNAME, CONTRACTID,
                               file, location, type);

    var catManager = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
    catManager.addCategoryEntry("net-content-sniffers",CLASSNAME,CONTRACTID,true,true);
  },

  unregisterSelf: function M_unregisterSelf(cm, location, type) {
     
     var catManager = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
     catManager.deleteCategoryEntry("net-content-sniffers",CLASSNAME,true);
     
     var cr = cm.QueryInterface(Components.interfaces.nsIComponentRegistrar);

     cr.registerFactoryLocation(CLASSID, location);
  },

  canUnload: function M_canUnload(cm) {
    return true;
  }
};

function NSGetModule(cm, file) {
  return Module;
}

